package DataStructures;
import java.util.ArrayList;
import java.util.Iterator;

import Blocks.GeneralBlock;

public class myTree<E> implements Tree {
	private ArrayList<E> elements = new ArrayList<E>();

	public ArrayList<E> getElements() {
		return elements;
	}

	public void setElements(ArrayList<E> elements) {
		this.elements = elements;
	}

	public boolean isLeftChlid(Object child) {
		GeneralBlock c = (GeneralBlock) child;
		int check = c.getIndex();
		return (!(check % 2 == 0));

	}

	@Override
	public int getParent(int child) {
		// TODO Auto-generated method stub
		return ((child - 1) / 2);

	}

	@Override
	public Object getRoot() {
		// TODO Auto-generated method stub
		return elements.get(0);
	}


	@Override
	public void setRoot(Object parent) {
		// TODO Auto-generated method stub
		E p = (E) parent;
		((GeneralBlock) p).setIndex(0);
		elements.add(0, p);
		insertLeftChild(0, null);
		insertLeftChild(0, null);
	}

	public void applyRule1(int parentIndex) {
		insertLeftChild(parentIndex, null);
		insertRightChild(parentIndex, null);
	}



	@Override
	public void insertLeftChild(int parentIndex, Object child) {

		// TODO Auto-generated method stub
		int index = calcLeft(parentIndex);
		if (child != null) {

			GeneralBlock myChild = (GeneralBlock) child;
			myChild.setIndex(index);
			elements.add(index, (E) myChild);
			// this condition to terminate calling this function

			// inserting null because array list is dynamic
			try{
			insertLeftChild(index, null);
			insertRightChild(index, null);
			}catch(Exception e){
				
			}
		} else {
			elements.add(index, null);
		}

	}

	@Override
	public void insertRightChild(int parentIndex, Object child) {
		// TODO Auto-generated method stub

		int index = calcRight(parentIndex);

		if (child != null) {
			GeneralBlock myChild = (GeneralBlock) child;
			// this condition to terminate calling this function
			myChild.setIndex(index);
			elements.add(index, (E) myChild);
			// inserting null because array list is dynamic
			try{
			insertLeftChild(index, null);
			insertRightChild(index, null);
			}catch(Exception e){
				
			}
		} else {
			elements.add(index, null);
		}

	}

	public Iterator<E> geIterator() {
		return elements.iterator();
	}

	@Override
	public boolean isLeaf(int child) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isLeftChlid(int child) {
		// TODO Auto-generated method stub
		return (!(child % 2 == 0));

	}

	@Override
	public boolean isRightChild(int child) {
		// TODO Auto-generated method stub
		return ((child % 2 == 0));

	}

	
	public int calcLeft(int parent) {
		return 2 * parent + 1;
	}

	public  int calcRight(int parent) {
		return 2 * parent + 2;
	}

	@Override
	public Object getLeftChild(int parent) {
		// TODO Auto-generated method stub

		return elements.get(calcLeft(parent));

	}

	

	@Override
	public Object getRightChild(int parent) {
		// TODO Auto-generated method stub
		return elements.get(calcRight(parent));
	}

	@Override
	public int getLevel(int index) {
		// TODO Auto-generated method stub
		return (int) (Math.log(index+1)/Math.log(2));
		
	}

	@Override
	public boolean isEmptyLevel(int levelIndex) {
		// TODO Auto-generated method stub
		System.out.println(levelIndex);
		int index = (int) (Math.pow(2,levelIndex)-1);
		System.out.println(index);
		int noOfele = (int) Math.pow(2, levelIndex+1)-1; 
		System.out.println(noOfele); 
		for(int i= index;i<noOfele;i++){
			if(elements.get(i)!=null){
				return false;
			}
		}
		return true;
	}
	


}
